package com.base.cn.platform.os.controller.op.article;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.service.manage.op.article.OpArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 资讯类型管理
 * @author zsw
 * @since 2018/5/4
 */
@Controller
@RequestMapping("/admin/op/article")
public class AdminOpArticleTypeController extends AdminBaseController {

    @Autowired
    private OpArticleTypeService opArticleTypeService;

    /**
     * 跳转资讯类型添加,修改页面
     * @param model
     * @param articleTypeId 资讯类型id
     * @return 资讯类型添加,修改页面
     */
    @RequestMapping(value = {"/toSaveUpdateArticleType"})
    public String saveUpdateArticleType(Model model,
                                    @RequestParam(value = "articleId", required = false, defaultValue = "0")BigDecimal articleTypeId){
        Map<String, Object> articleType = null;
        if(articleTypeId.compareTo(new BigDecimal(0)) > 0){
            articleType = opArticleTypeService.findArticleTypeById(articleTypeId);
        }
        if(articleType == null){
            articleType = new HashMap<>();
        }
        model.addAttribute("articleType", articleType);
        return resultPage("/op/article/save-update-articleType");
    }

    /**
     * 保存资讯类型
     * @param articleType 资讯类型实体
     * @return
     */
    @RequestMapping("/saveUpdateArticleType")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "保存或修改资讯类型")
    public Map<String, Object> saveOrUpdateArticleType(@RequestBody Map<String,Object> articleType){
        return opArticleTypeService.saveUpdateArticleType(articleType);
    }

    /**
     * 搜索资讯类型列表
     * @param model
     * @return 资讯类型列表
     */
    @RequestMapping("/queryArticleTypeList")
    public String queryArticleTypeList(Model model){
        return resultPage("/op/article/articleType-list");
    }

    /**
     * ajax搜索资讯类型列表
     * @param model
     * @param currentPage 当前页码
     * @param params 查询条件
     * @return 资讯类型列表页面
     */
    @RequestMapping("/queryAjaxArticleTypeList")
    public String queryAjaxArticleTypeList(Model model,
                                       @RequestParam(value = "currentPage", required = false,defaultValue = "1")int currentPage,
                                       @RequestBody Map<String, Object> params){
        Map<String, Object> dataMap = opArticleTypeService.queryArticleTypeList(currentPage, params);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/op/article/articleType-ajax-list");
    }

    /**
     * 根据资讯类型id批量操作 修改，恢复
     * @param articleTypeIds 资讯类型id串
     * @param status 1正常 2禁用
     * @return
     */
    @RequestMapping("/updateArticleTypeStatusByIds")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改资讯类型状态")
    public Map<String,Object> updateArticleTypeStatusByIds(@RequestParam("articleTypeIds")String articleTypeIds,
                                                       @RequestParam("status")Integer status){
        return opArticleTypeService.updateArticleTypeStatusByIds(articleTypeIds,status);
    }

}
